#ecoding: utf-8 -*-
WORKPATH = ""
APPS_DIR = ""
import os,logging

def load():
    """初始化lib路径，apps路径"""
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    global WORKPATH, APPS_DIR
    #添加libs目录
    WORKPATH = os.path.dirname(__file__)
    sys.path.append(os.path.join(WORKPATH, 'libs'))
    APPS_DIR = os.path.join(WORKPATH, 'apps')
    sys.path.append(APPS_DIR)
load()

TEMPLATE_ROOT = os.path.join(WORKPATH, 'templates')
STATIC_ROOT = os.path.join(TEMPLATE_ROOT, 'static')
    
logging.basicConfig(level = logging.DEBUG,
    format = '%(asctime)s %(levelname)s %(module)s.%(funcName)s Line:%(lineno)d \t %(message)s')

dirs_config = {'template_root':TEMPLATE_ROOT, "static_root":STATIC_ROOT, 'apps_dir':APPS_DIR}

SITE_CONFIG = {'dirs_config':dirs_config}

from pynixweb import config
config.config = SITE_CONFIG

mapper = {}
from pynixweb import utils
import os
def init():
    """自动配置apps下面的工程"""
    commons_dirs = os.listdir(config.config["dirs_config"]["apps_dir"])
    for one_file in commons_dirs:
        one_path = os.path.join(config.config["dirs_config"]["apps_dir"], one_file)
        if (os.path.isdir(one_path)) and (not one_file.startswith('.')):
            #try:
                module =  __import__(one_file)
                if hasattr(module, "getConfig"):
                    utils.addDict(config.config, module.getConfig())
                    logging.info("load config from %s"% one_file)
                if hasattr(module, "getMapper"):
                    utils.addDict(mapper, module.getMapper())
                    logging.info("load mapping from %s"% one_file)
            #except Exception, e:
                #logging.error("%s load error.\n error:%s"%(one_file,e))

init()